package com.ruoyi.web.controller.wechat;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.constant.wechat.WechatConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.wechat.WechatInspectionProblem;
import com.ruoyi.common.enums.wechat.ProblemStatus;
import com.ruoyi.common.enums.wechat.WechatRectificationStatusEnum;
import com.ruoyi.common.exception.user.UserException;
import com.ruoyi.system.domain.wechat.request.vo.FilterForWechatInspectionProblemListVO;
import com.ruoyi.system.domain.wechat.request.vo.WechatInspectionProblemModifyVO;
import com.ruoyi.system.domain.wechat.request.vo.WechatInspectionProblemReportVO;
import com.ruoyi.system.domain.wechat.request.vo.WechatInspectionProblemReviewVO;
import com.ruoyi.system.service.wechat.IWechatInspectionProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 微信小程序巡检问题
 *
 * @author jiexiang.sun/Jason
 * @version 1.0.0
 * @create 2022-03-09 19:59
 **/
@RestController
@Validated
@RequestMapping("/wechat/problem")
public class WechatInspectionProblemController extends BaseController {

    @Autowired
    private IWechatInspectionProblemService wechatInspectionProblemService;

    /**
     * 统计巡检问题总数
     *
     * @param userId
     * @param userRoleId
     * @param userDeptId
     * @param startTime
     * @param endTime
     * @param params
     * @return
     */
    @GetMapping("statisticsProblemCount")
    public AjaxResult statisticsProblemCount(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId
            , Long startTime, Long endTime
            , FilterForWechatInspectionProblemListVO params) {
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        if (ObjectUtil.isNotNull(startTime)) {
            params.setStartTime(DateUtil.date(startTime));
        }
        if (ObjectUtil.isNotNull(endTime)) {
            params.setEndTime(DateUtil.date(endTime));
        }
        return AjaxResult.success(wechatInspectionProblemService.statisticsProblemCount(params));
    }

    /**
     * 问题列表查询
     *
     * @param userId
     * @param params
     * @return
     */
    @GetMapping("getList")
    public AjaxResult getList(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId
            , Long startTime, Long endTime
            , FilterForWechatInspectionProblemListVO params) {
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        if (ObjectUtil.isNotNull(startTime)) {
            params.setStartTime(DateUtil.date(startTime));
        }
        if (ObjectUtil.isNotNull(endTime)) {
            params.setEndTime(DateUtil.date(endTime));
        }
        return AjaxResult.success(wechatInspectionProblemService.getList(params));
    }

    /**
     * 查询所有问题
     *
     * @return
     */
    @GetMapping("getAllList")
    public AjaxResult getAllList(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , FilterForWechatInspectionProblemListVO params) {
        params.setUserRoleId(userRoleId);
        return AjaxResult.success(wechatInspectionProblemService.getAllList(params));
    }

    /**
     * 查询不在任务中的问题
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getByOtherTask")
    public AjaxResult getByOtherTask(Long startTime, Long endTime
            , FilterForWechatInspectionProblemListVO params) {
        if (ObjectUtil.isNotNull(startTime)) {
            params.setStartTime(DateUtil.date(startTime));
        }
        if (ObjectUtil.isNotNull(endTime)) {
            params.setEndTime(DateUtil.date(endTime));
        }
        return AjaxResult.success(wechatInspectionProblemService.getByOtherTask(params));
    }

    /**
     * 统计审批总数
     *
     * @param userId
     * @param userRoleId
     * @param userDeptId
     * @param startTime
     * @param endTime
     * @param params
     * @return
     */
    @GetMapping("statisticsApprovalCount")
    public AjaxResult statisticsApprovalCount(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId
            , Long startTime, Long endTime
            , FilterForWechatInspectionProblemListVO params) {
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        if (ObjectUtil.isNotNull(startTime)) {
            params.setStartTime(DateUtil.date(startTime));
        }
        if (ObjectUtil.isNotNull(endTime)) {
            params.setEndTime(DateUtil.date(endTime));
        }
        return AjaxResult.success(wechatInspectionProblemService.statisticsApprovalCount(params));
    }

    /**
     * 根据行政地区-区县统计问题发布数量分布
     * @return
     */
    @GetMapping("statisticsByProblemToArea")
    public AjaxResult statisticsByProblemToArea(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId){
        FilterForWechatInspectionProblemListVO params = new FilterForWechatInspectionProblemListVO();
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        return AjaxResult.success(wechatInspectionProblemService.statisticsByProblemToArea(params));
    }

    /**
     * 根据行政地区-区县统计问题发布数量分布
     * @return
     */
    @GetMapping("statisticsByProblemToStreet")
    public AjaxResult statisticsByProblemToStreet(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId){
        FilterForWechatInspectionProblemListVO params = new FilterForWechatInspectionProblemListVO();
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        return AjaxResult.success(wechatInspectionProblemService.statisticsByProblemToStreet(params));
    }

    /**
     * 根据行政地区-区县统计问题类型发布数量分布
     * @return
     */
    @GetMapping("statisticsByProblemTypeToArea")
    public AjaxResult statisticsByProblemTypeToArea(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId){
        FilterForWechatInspectionProblemListVO params = new FilterForWechatInspectionProblemListVO();
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        return AjaxResult.success(wechatInspectionProblemService.statisticsByProblemTypeToArea(params));
    }

    /**
     * 根据行政地区-区县统计问题类型发布数量分布
     * @return
     */
    @GetMapping("statisticsByProblemTypeToStreet")
    public AjaxResult statisticsByProblemTypeToStreet(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId){
        FilterForWechatInspectionProblemListVO params = new FilterForWechatInspectionProblemListVO();
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        return AjaxResult.success(wechatInspectionProblemService.statisticsByProblemTypeToStreet(params));
    }

    /**
     * 审批问题列表查询
     *
     * @param params
     * @return
     */
    @GetMapping("getApprovalList")
    public AjaxResult getApprovalList(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId
            , Long startTime, Long endTime
            , FilterForWechatInspectionProblemListVO params) {
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        if (ObjectUtil.isNotNull(startTime)) {
            params.setStartTime(DateUtil.date(startTime));
        }
        if (ObjectUtil.isNotNull(endTime)) {
            params.setEndTime(DateUtil.date(endTime));
        }
        return AjaxResult.success(wechatInspectionProblemService.getApprovalList(params));
    }

    /**
     * 统计问题工单总数
     *
     * @param userId
     * @param userRoleId
     * @param userDeptId
     * @param startTime
     * @param endTime
     * @param params
     * @return
     */
    @GetMapping("statisticsProblemWorkOrderCount")
    public AjaxResult statisticsProblemWorkOrderCount(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId
            , Long startTime, Long endTime
            , FilterForWechatInspectionProblemListVO params) {
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        if (ObjectUtil.isNotNull(startTime)) {
            params.setStartTime(DateUtil.date(startTime));
        }
        if (ObjectUtil.isNotNull(endTime)) {
            params.setEndTime(DateUtil.date(endTime));
        }
        return AjaxResult.success(wechatInspectionProblemService.statisticsProblemWorkOrderCount(params));
    }

    /**
     * 获取问题工单列表
     *
     * @param userId
     * @param userRoleId
     * @param userDeptId
     * @param startTime
     * @param endTime
     * @param params
     * @return
     */
    @GetMapping("getWorkOrderList")
    public AjaxResult getWorkOrderList(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId
            , Long startTime, Long endTime
            , FilterForWechatInspectionProblemListVO params) {
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setUserDeptId(userDeptId);
        if (ObjectUtil.isNotNull(startTime)) {
            params.setStartTime(DateUtil.date(startTime));
        }
        if (ObjectUtil.isNotNull(endTime)) {
            params.setEndTime(DateUtil.date(endTime));
        }
        return AjaxResult.success(wechatInspectionProblemService.getWorkOrderList(params));
    }

    /**
     * 问题详情查询
     *
     * @param problemId
     * @return
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo(@NotBlank(message = "问题编号不能为空") String problemId) {
        return AjaxResult.success(wechatInspectionProblemService.getInfo(problemId));
    }

    /**
     * 巡检问题上报
     *
     * @param inspectionProblem
     * @param userId
     * @return
     */
    @PostMapping("report")
    public AjaxResult report(@Validated @RequestBody WechatInspectionProblemReportVO inspectionProblem
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId) {
        inspectionProblem.setReportUserId(userId);
        inspectionProblem.setReportUserRoleId(userRoleId);
        inspectionProblem.setReportUserDeptId(userDeptId);
        if (wechatInspectionProblemService.report(inspectionProblem)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 巡检问题下发
     *
     * @param deptIds
     * @param problemIds
     * @return
     */
    @PostMapping("publish")
    public AjaxResult publish(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @NotBlank(message = "请选择下发部门") String deptIds
            , @NotBlank(message = "问题编号不能为空") String problemIds) {
        //当前token能不能下发问题，只有市级、区县级角色用户才有权限下发
        if (userRoleId.longValue() != 1 && userRoleId.longValue() != 2) {
            throw new UserException("wechat.user.no.publish.permission", null);
        }

        if (wechatInspectionProblemService.publish(userId, userRoleId, deptIds, problemIds)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 巡检问题整改提交
     *
     * @param userId
     * @param problemId
     * @param files
     * @return
     */
    @PostMapping("rectification")
    public AjaxResult rectification(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @NotNull(message = "问题编号不能为空") Long problemId, String problemPhoto) {
        if (userRoleId.longValue() != 3) {
            throw new UserException("wechat.user.no.rectification.permission", null);
        }

        if (wechatInspectionProblemService.rectification(userId, problemId, problemPhoto)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 巡检问题整改审核
     *
     * @param userId
     * @param problemId
     * @return
     */
    @PostMapping("audit")
    public AjaxResult audit(@RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @NotNull(message = "问题编号不能为空") Long problemId
            , @NotNull(message = "审核状态不能为空") Boolean status) {
        if (userRoleId.longValue() != 1 && userRoleId.longValue() != 2) {
            throw new UserException("wechat.user.no.audit.permission", null);
        }

        short problemStatus = 0;
        if (status) {
            problemStatus = WechatRectificationStatusEnum.PROCESSING_COMPLETE.getStatus();
        } else {
            if (userRoleId.longValue() == 1) {
                problemStatus = WechatRectificationStatusEnum.HANDLING_RETURN_COUNTY.getStatus();
            } else if (userRoleId.longValue() != 2) {
                problemStatus = WechatRectificationStatusEnum.HANDLING_RETURN_STREET.getStatus();
            }
        }

        if (wechatInspectionProblemService.audit(userId, problemId, problemStatus)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 问题删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        boolean result;
        if (StrUtil.isNotBlank(id)) {
            LambdaUpdateWrapper<WechatInspectionProblem> uw = new LambdaUpdateWrapper<>();
            uw.in(WechatInspectionProblem::getId, Arrays.stream(id.split(",")).collect(Collectors.toList()));
            WechatInspectionProblem update = new WechatInspectionProblem();
            update.setStatus(ProblemStatus.DELETED.getCode());
            result = wechatInspectionProblemService.update(update, uw);
        } else {
            throw new UserException("wechat.problem.delete.id.isblank", null);
        }
        return AjaxResult.success(result);
    }

    /**
     * 问题修改
     *
     * @param inspectionProblem
     * @param userId
     * @return
     */
    @PutMapping("modify")
    public AjaxResult modify(@Validated @RequestBody WechatInspectionProblemModifyVO inspectionProblem
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId) {
        wechatInspectionProblemService.modify(userId, inspectionProblem);
        return AjaxResult.success();
    }

    /**
     * 导出csv问题清单
     *
     * @param taskId
     * @param userId
     * @param userRoleId
     * @return
     */
    @GetMapping("export/{taskId}")
    public void export(@PathVariable Long taskId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , HttpServletResponse response) {
        FilterForWechatInspectionProblemListVO params = new FilterForWechatInspectionProblemListVO();
        params.setUserId(userId);
        params.setUserRoleId(userRoleId);
        params.setTaskId(taskId);
        wechatInspectionProblemService.export(params, response);
    }

    /**
     * 问题复检
     *
     * @param problemId
     * @param taskId
     * @param userId
     * @param userRoleId
     * @param userDeptId
     * @return
     */
    @PostMapping("review/{problemId}")
    public AjaxResult review(@PathVariable("problemId") String problemId, @NotNull(message = "请指定任务") Long taskId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USERID) Long userId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_ROLEID) Long userRoleId
            , @RequestAttribute(name = WechatConstants.WECHAT_TOKEN_USER_DEPTID) Long userDeptId) {

        WechatInspectionProblemReviewVO inspectionProblem = new WechatInspectionProblemReviewVO();
        inspectionProblem.setReportUserId(userId);
        inspectionProblem.setReportUserRoleId(userRoleId);
        inspectionProblem.setReportUserDeptId(userDeptId);
        inspectionProblem.setTaskId(taskId);
        inspectionProblem.setProblemId(problemId);
        return wechatInspectionProblemService.review(inspectionProblem) ? AjaxResult.success() : AjaxResult.error();
    }
}
